package com.example.task1.ui.AllService.logistics;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LogisticsOrderBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class LogisticsOrderActivity extends AppCompatActivity {

    private TextView tvOrderNum;
    private TextView tvOrderTime;
    private LinearLayout intent;
    private TextView tvLogistics;
    private RecyclerView rvList;
    private TextView tvPhone;
    private Button btnSend;
    private RelativeLayout tvRecord;
    private LogisticsOrderBean.DataDTO data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_order);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("订单详情");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        data = MyApplication.logisticsOrderBean.getData();
        initView();
        tvOrderNum.setText("订单号：" + data.getInfoNo());
        tvLogistics.setText("承担物流公司：" + data.getCompany().getName());
        tvOrderTime.setText("订单创建时间：" + data.getCreateTime());
        tvPhone.setText("投诉电话：" + data.getCompany().getPhone());
        intent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(LogisticsOrderActivity.this, LogisticsDetailsActivity.class, data.getCompanyId());
            }
        });
        LogisticsOrderAdapter orderAdapter = new LogisticsOrderAdapter(data.getStepList(), this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(orderAdapter);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LogisticsOrderActivity.this);
                builder.setTitle("请输入投诉内容");
                EditText editText = new EditText(LogisticsOrderActivity.this);
                builder.setView(editText);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = editText.getText().toString();
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("companyId",data.getCompanyId());
                        map.put("infoNo",data.getInfoNo());
                        map.put("description",s);
                        Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/logistics-inquiry/logistics_complaint", Api.getBody(map));
                        Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                MyApplication.show(data.getMsg());
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    }
                });
                builder.show();
            }
        });
        tvRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(LogisticsOrderActivity.this,LogisticsComplaintActivity.class);
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.logisticsOrderBean = null;
    }

    private void initView() {
        tvOrderNum = (TextView) findViewById(R.id.tv_order_num);
        tvOrderTime = (TextView) findViewById(R.id.tv_order_time);
        intent = (LinearLayout) findViewById(R.id.intent);
        tvLogistics = (TextView) findViewById(R.id.tv_logistics);
        rvList = (RecyclerView) findViewById(R.id.rv_list);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        btnSend = (Button) findViewById(R.id.btn_send);
        tvRecord = (RelativeLayout) findViewById(R.id.tv_record);
    }
}